package com.yinshu.teleshield.service.impl;

import cn.hutool.core.lang.Assert;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yinshu.teleshield.domain.entity.UserEntity;
import com.yinshu.teleshield.domain.vo.user.UserPageQuery;
import com.yinshu.teleshield.domain.vo.user.UserResultVO;
import com.yinshu.teleshield.domain.vo.user.UserSaveVO;
import com.yinshu.teleshield.domain.vo.user.UserUpdateVO;
import com.yinshu.teleshield.manager.UserManager;
import com.yinshu.teleshield.service.UserService;
import com.yinshu.teleshield.tkbase.request.PageParams;
import com.yinshu.teleshield.tkbase.service.impl.SuperServiceImpl;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Objects;


/**
 * 用户表(User)表服务实现类
 *
 * @author yangjunxiong   2024-11-11 11:22:22
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class UserServiceImpl extends SuperServiceImpl<UserManager, Long, UserEntity> implements UserService {
    @Override
    protected <SaveVO> UserEntity saveBefore(SaveVO saveVO) {
        UserSaveVO saveBeforeVO = (UserSaveVO) saveVO;
        UserEntity entity = super.saveBefore(saveBeforeVO);
        return entity;
    }

    @Override
    protected <UpdateVO> UserEntity updateBefore(UpdateVO updateVO) {
        UserUpdateVO updateBeforeVO = (UserUpdateVO) updateVO;
        UserEntity byId = this.getById(updateBeforeVO.getId());
        Assert.notNull(byId, "用户不存在");
        Assert.isTrue(byId.getPhone().equals(updateBeforeVO.getPhone()), "手机号不能修改");
        UserEntity entity = super.updateBefore(updateBeforeVO);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean logicRemoveByIds(Collection<Long> idList) {
        return super.logicRemoveByIds(idList);
    }

    @Override
    public <ResultVO> void handlerListResult(List<ResultVO> resultVO) {
        List<UserResultVO> list = (List<UserResultVO>) resultVO;
        list.stream().filter(Objects::nonNull).forEach(item -> {
        });
    }

    @Override
    public <PageQuery> MPJLambdaWrapper<UserEntity> handlerWrapper(MPJLambdaWrapper<UserEntity> queryWrap, PageParams<PageQuery> params) {
        UserPageQuery model = (UserPageQuery) params.getModel();
        queryWrap.eq(UserEntity::getDeleted, 0);
        return queryWrap;
    }

    //连表查询，则重写该分页方法，否则走默认单表查询
    //@Override
    //public <ResultVO, PageQuery> IPage<ResultVO> page(PageParams<PageQuery> params, Class<ResultVO> clazz) {
    //    UserPageQuery model = (UserPageQuery) params.getModel();
    //    MPJLambdaWrapper<UserEntity> wrapper = handlerWrapper(new MPJLambdaWrapper<UserEntity>()
    //        .selectAll(UserEntity.class)//查询主表全部字段
    //        .selectAs(TaskAuto::getId, UserResultVO::getTaskId)
    //        .selectAs(TaskAuto::getCreateTime, UserResultVO::getTaskCreateTime)
    //        .select(TaskAuto::getId, TaskAuto::getTaskName, TaskAuto::getCreateTime)
    //               .leftJoin(TaskAuto.class, TaskAuto::getId, UserEntity::getId)
    //               .eq(TaskAuto::getSolverCode, "S_2024062016530027")
    //               .leftJoin(Task.class, Task::getId, TaskAuto::getId)
    //               .eq(Task::getSolverCode, "S_2024062016530027")
    //        , params);
    //    IPage<UserResultVO>  page = this.superManager.selectJoinListPage(params.buildPage(UserEntity.class), UserResultVO.class, wrapper);
    //    return CommonBeanUtils.toBeanPage(page, clazz);

    //}

}
